<template>
  <div class="page-container">
    <div class="card-title" v-show="showSearch">
      <el-form size="small" label-width="68px" :model="queryParams" ref="queryForm">
        <el-row :gutter="48">
          <el-col :md="6" :sm="24">
            <el-form-item label="文章名称">
              <el-input v-model="queryParams.title" class="search-item" placeholder="请输入文章名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="文章类型">
              <el-select v-model="queryParams.type" placeholder="请选择文章类型" class="search-item" clearable>
                <el-option v-for="dict in dict.type.blog_article_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="可见范围">
              <el-select v-model="queryParams.visibility" placeholder="请选择可见范围" class="search-item" clearable>
                <el-option v-for="dict in dict.type.blog_visible_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24" v-if="advanced">
            <!-- 发布时间 -->
            <el-form-item label="发布时间">
              <el-date-picker
                v-model="queryParams.dataRange"
                class="search-item"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <div class="search-content">
          <!-- 搜索按钮 -->
          <el-form-item class="search-button">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
            <el-button
                type="text"
                :icon="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                class="advanced-button"
                @click="toggleAdvanced">
                {{ advanced ? '关闭' : '展开' }}
              </el-button>
          </el-form-item>
          </div>
        </el-row>
      </el-form>
    </div>
    <div class="card">
      <el-row :gutter="10" class="button-container">
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" plain :disabled="multiple" @click="handleDelete" :loading="buttonLoading">删除草稿</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" :columns="columns" @getTableData="getTableData"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" element-loading-text="正在加载表格数据" :data="tableData" @selection-change="handleSelectionChange" border>
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column type="selection" width="50" align="center" />
        <!-- 文章名称 -->
        <el-table-column prop="title" label="文章名称" align="center" v-if="columns[0].visible" show-overflow-tooltip></el-table-column>
        <!-- 文章封面 -->
        <el-table-column prop="cover" label="文章封面" align="center" v-if="columns[1].visible">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover">
              <div slot="error" class="image-slot-table">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <!-- 文章类型 -->
        <el-table-column prop="type" label="文章类型" align="center" v-if="columns[2].visible">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 0" type="primary">原创</el-tag>
            <el-tag v-else-if="scope.row.type === 1" type="danger">转载</el-tag>
            <el-tag v-else type="success">翻译</el-tag>
          </template>
        </el-table-column>
        <!-- 可见范围 -->
        <el-table-column prop="visibility" label="可见范围" align="center" v-if="columns[3].visible">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.visibility === 0" type="primary">私密</el-tag>
            <el-tag v-else-if="scope.row.visibility === 1" type="success">公开</el-tag>
          </template>
        </el-table-column>
        <!-- 允许评论 -->
        <el-table-column prop="allowComment" label="评论" align="center" v-if="columns[4].visible">
          <template slot-scope="scope">
            <el-switch
              @change="handleSettings('COMMENT', scope.row.id)"
              v-model="scope.row.allowComment"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <!-- 是否置顶 -->
        <el-table-column prop="isSticky" label="置顶" align="center" v-if="columns[5].visible">
          <template slot-scope="scope">
            <el-switch
              @change="handleSettings('STICKY', scope.row.id)"
              v-model="scope.row.isSticky"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <!-- 打赏 -->
        <el-table-column prop="allowDonation" label="打赏" align="center" v-if="columns[6].visible">
          <template slot-scope="scope">
            <el-switch
              @change="handleSettings('DONATION', scope.row.id)"
              v-model="scope.row.allowDonation"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-edit" @click="handleEdit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <pagination
        :background="false"
        :total="total"
        :page.sync="queryParams.currentPage"
        :limit.sync="queryParams.pageSize"
        @pagination="getTableData"
      />
    </div>

  </div>
</template>
<script>
import Pagination from '../../../../../components/Pagination/index.vue'
import RightToolbar from '../../../../../components/RightToolbar/index.vue'
import { listArticles, getArticleById, deleteArticle, updateSettings } from '../../../../../api/blog/article'

export default {
  name: 'Article',

  dicts: ['blog_article_type', 'blog_visible_type'],

  components: { Pagination, RightToolbar },

  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 是否展示搜索内容
      showSearch: true,
      // 数据总条数
      total: 0,
      // 文章对话框是否显示
      articleVisible: false,
      // 数据表格等待框
      loading: false,
      // 查询参数
      queryParams: {
        // 页数
        currentPage: 1,
        // 页面大小
        pageSize: 10,
        // 文章名称
        title: undefined,
        // 文章类型
        type: undefined,
        // 可见范围
        visibility: undefined,
        // 文章状态
        status: undefined,
        // 时间范围
        dataRange: []
      },
      // 对话框按钮等待框
      buttonLoading: false,
      // 列信息
      columns: [
        // 文章名称
        { key: 0, label: '文章名称', visible: true },
        // 文章封面
        { key: 1, label: '文章封面', visible: true },
        // 文章类型
        { key: 2, label: '文章类型', visible: true },
        // 可见范围
        { key: 3, label: '可见范围', visible: true },
        // 允许评论
        { key: 4, label: '评论', visible: true },
        // 是否置顶
        { key: 5, label: '置顶', visible: true },
        // 打赏
        { key: 6, label: '打赏', visible: true }
      ],
      // 表格数据
      tableData: [],
      // 表头选中的数据
      ids: [],
      // 多数据禁用
      single: true,
      // 单数据禁用
      multiple: true,
      // 对话框标题
      articleTitle: ''
    }
  },

  mounted () {
    // 文章状态为已发布
    this.queryParams.status = 0
    // 查询表格数据
    this.getTableData()
  },

  methods: {
    // 获取表格数据
    getTableData () {
      // 开启加载框
      this.loading = true
      listArticles(this.queryParams).then((res) => {
        // 赋值数据参数
        this.tableData = res.list
        this.total = res.total
        // 关闭等待框
        this.loading = false
      }).catch(() => {
        // 确保加载框可以关闭
        this.loading = false
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 点击查询按钮
    handleQuery () {
      this.queryParams.currentPage = 1
      this.getTableData()
    },
    // 点击重置按钮
    handleReset () {
      // 文章名称
      this.queryParams.title = ''
      // 文章类型
      this.queryParams.type = undefined
      // 可见范围
      this.queryParams.visibility = undefined
      this.queryParams.dataRange = []
    },
    // 点击编辑按钮
    handleUpdate (row) {
      // 修改对话框标题
      this.articleTitle = '更新文章'
      // 获取到传来的ID
      const articleId = row.id || this.ids
      // 打开加载框
      this.articleLoading = true
      // 跳转到文章编辑页面
      // 根据文章ID获取对应的数据
      getArticleById(articleId).then((res) => {
        // 赋值当前的文章数据
        this.formData = res
        // 打开编辑文章对话框
        this.articleVisible = true
        // 关闭加载框
        this.articleLoading = false
      })
    },
    // 点击删除事件
    handleDelete (row) {
      const that = this
      let articleIds = []
      if (row.id) {
        articleIds.push(row.id)
      } else {
        articleIds = this.ids
      }
      this.$confirm('是否确认删除选中的文章数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.buttonLoading = true
        that.loading = true
        deleteArticle(articleIds).then((res) => {
          that.buttonLoading = false
          that.$message.success('删除成功')
          // 刷新数据
          that.getTableData()
        }).catch(() => {
          // 确保加载框会关闭
          that.buttonLoading = false
          that.loading = false
        })
      }).catch(() => {})
    },
    handleEdit (id) {
      this.$router.push('/article-content/article-edit/' + id)
    },
    // 多选框
    handleSelectionChange (selection) {
      // 获取当前选中数组的ID
      this.ids = selection.map(item => item.id)
      // 表头的编辑是否可以点击
      this.single = selection.length !== 1
      // 表头的删除是否可以点击
      this.multiple = !selection.length
    },
    // 更新状态属性
    handleSettings (settings, id) {
      // 加载等待框
      this.loading = true
      updateSettings({ id, settings }).then((res) => {
        // 输出提示信息
        this.$message.success('状态修改成功')
        // 关闭加载框
        this.loading = false
      }).catch(() => {
        // 关闭加载框
        this.loading = false
      })
    }
  }
}

</script>

<style lang="scss" scoped>
.article-wrapper {
  padding: 10px 30px;
}
</style>
